package com.caigou.modules.famous.ui.personal.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bean.AppBackgroundInfo
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.famous.R
import com.caigou.modules.famous.adpter.PersonalItemAdapter
import com.caigou.modules.famous.bean.PersonalBean
import com.caigou.modules.famous.databinding.FragmentPersonalShaBangBinding
import com.caigou.modules.famous.ui.personal.FamousPersonalActivity
import com.caigou.modules.famous.viewmodel.PersonalViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/10
 */
@AndroidEntryPoint
class PersonalShaBangFragment : BaseFragment() {

    private var _binding: FragmentPersonalShaBangBinding? = null
    private val binding get() = _binding!!

    private val personalViewModel: PersonalViewModel by viewModels()

    private var dareTypeId = 34

    private val mAdapter: PersonalItemAdapter by lazy {
        PersonalItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentPersonalShaBangBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.shaBangSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchContestList()
            }
        }

        binding.shaBangRecyclerView.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        binding.shaBangRgSelector.setOnCheckedChangeListener { group, checkedId ->
            dareTypeId = when (checkedId) {
                binding.shaBangBtnShaYiXiao.id -> 34
                binding.shaBangBtnSha5ma.id -> 35
                binding.shaBangBtnShaYiTou.id -> 36
                binding.shaBangBtnShaYiWei.id -> 37
                binding.shaBangBtnShaYiDuan.id -> 38
                binding.shaBangBtnShaYiHang.id -> 39
                binding.shaBangBtnShaErWei.id -> 40
                binding.shaBangBtnShaErXiao.id -> 41
                binding.shaBangBtnShaBanBo.id -> 42
                else -> 34
            }
            fetchContestList()
        }


        LiveDataBus.get().with(BusKey.FAMOUS_TYPE_CHANGE, Int::class.java).observe(this) {
            fetchContestList()
        }
    }

    override fun lazyLoad() {
        fetchContestList()
    }

    override fun onBack() {

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(personalViewModel.shaBangListLiveData, ::handleGetTeXiaoContestList)
    }

    private fun handleGetTeXiaoContestList(result: Resource<PersonalBean>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.shaBangSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取特肖列表异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.shaBangSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!.contestList)
                (requireActivity() as FamousPersonalActivity).handleHeaderUI(result.data!!)
                (requireActivity() as FamousPersonalActivity).handleFooterUI(result.data!!)
            }
        }
    }

    private fun fetchContestList() {
        val currentUserId = if (AppBackgroundInfo.instance.hasLogin) {
            AppBackgroundInfo.instance.userBean!!.userId
        } else {
            ""
        }
        val famousType = (requireActivity() as FamousPersonalActivity).famousType
        val userId = (requireActivity() as FamousPersonalActivity).userId
        personalViewModel.getShaBangContestList(dareTypeId, famousType, userId, currentUserId)
    }
}